package chapter4.homework;

/**
 * @Project ：learn_java
 * @File ：Q6.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/10 20:47
 * @Description ：For study.
 */

class Tunnel implements Runnable {
    private int crossNum = 0;

    @Override
    public void run() {
        cross();
    }

    private synchronized void cross() {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + "开始通过隧道...");
        try {
            Thread.sleep(5000); // 模拟通过隧道耗时5秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        crossNum++;
        System.out.println(threadName + "已经通过隧道，TA是第" + crossNum + "通过的！");
    }
}

public class Q6 {
    public static void main(String[] args) {
        Tunnel tunnel = new Tunnel();
        for (int i = 1; i <= 10; i++) {
            Thread thread = new Thread(tunnel, "person" + i);
            thread.start();
        }
    }
}
